/**
 * @description 收藏 services
 */

const {
    Follow,
    User
} = require('../db/model/index')
const {
    Sequelize
} = require('sequelize')

/**
 * @description 查询是否收藏
 * @param {number} userId 添加收藏的用户id
 * @param {number} followId 被收藏的用户id
 */

async function getFollowInfo({
    userId,
    followId
}) {
    const result = await Follow.findOne({
        where: {
            userId,
            followId
        }
    })
    console.log(JSON.stringify(result), 'result')
    return result
}

/**
 * @description 添加收藏
 * @param {number} userId 添加收藏的用户id
 * @param {number} followId 被收藏的用户id
 */
async function addFollowData({
    userId,
    followId
}) {
    const result = await Follow.create({
        userId,
        followId
    })
    return result
}

/**
 * @description 取消收藏
 * @param {number} userId 添加收藏的用户id
 * @param {number} followId 被收藏的用户id
 */
async function cancelFollowData({
    userId,
    followId
}) {
    const result = await Follow.destroy({
        where: {
            userId,
            followId
        }
    })
    console.log(JSON.stringify(result));
    return result > 0
}

/**
 * @description 收藏列表
 * @param {number} userId 要查询用户的Id
 */
async function getFollowData(userId) {
    const result = await Follow.findAndCountAll({
        attributes:['userId','followId',Sequelize.col('user.id'),Sequelize.col('user.userName'),Sequelize.col('user.nickName'),Sequelize.col('user.avatar'),Sequelize.col('user.cover'),Sequelize.col('user.types')],
        order: [
            ['id', 'desc']
        ],
        where: {
            userId
        },
        include: [{
            model: User,
            attributes: []
            // attributes: [sequelize.col('user.id'), 'userName', 'nickName', 'avatar', 'cover', 'types']
        }],
        raw: true
    })
    return result
}


module.exports = {
    addFollowData,
    getFollowInfo,
    cancelFollowData,
    getFollowData
}